package cn.lkp.mall.product.service.impl;

import cn.lkp.mall.product.entity.AttrAttrgroupRelationEntity;
import cn.lkp.mall.product.entity.AttrEntity;
import cn.lkp.mall.product.entity.CategoryEntity;
import cn.lkp.mall.product.service.AttrService;
import cn.lkp.mall.product.vo.AttrGroupRelationVo;
import cn.lkp.mall.product.vo.AttrGroupWithAttrsVo;
import cn.lkp.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lkp.common.utils.PageUtils;
import cn.lkp.common.utils.Query;

import cn.lkp.mall.product.dao.AttrGroupDao;
import cn.lkp.mall.product.entity.AttrGroupEntity;
import cn.lkp.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        /*
         * 1. 如果catId为0, 且key无值, 给所有数据
         * 2. 如果catId为0, 且key有值, 按照key搜索
         * 3. 如果catId不为0, 且key无值, 按照catId查数据
         * 4. 如果catId不为0, 且key有值, 按照key和catId条件同时查
         */
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        IPage<AttrGroupEntity> page = null;
        String key = (String) params.get("key");
        if(catelogId != 0){
            wrapper.eq("catelog_id",catelogId);
        }
        //按照三级分类查找
        //使用spring的StringUtils工具类
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    /**
     * 根据三级分类Id查询出所有分组及分组下的所有属性
     * @param catelogId
     * @return
     */
    @Autowired
    AttrService attrService;
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2.查询分组下的所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntityList.stream().map((item) -> {
            AttrGroupWithAttrsVo AttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, AttrsVo);
            Long attrGroupId = AttrsVo.getAttrGroupId();
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupId);
            AttrsVo.setAttrs(attrEntities);
            return AttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> attrGroupVos = this.baseMapper.getAttrGroupWithAttrsByIds(spuId,catalogId);
        return attrGroupVos;
    }
}